package com.leetcode;

import java.util.HashMap;
import java.util.Map;

public class LengthOfLongestSubstring_3 {
    public int lengthOfLongestSubstring(String s) {
        int left = 0, right = 0, res = 0;
        Map<Character, Integer> window = new HashMap<>();

        while (right < s.length()) {
            char d = s.charAt(right);
            right++;
            window.put(d, window.getOrDefault(d, 0) + 1);
            while (window.get(d) > 1) {
                char c = s.charAt(left);
                window.put(c,window.get(c) - 1);
                left++;
            }
            res = Math.max(res,right - left);
        }
        return res;
    }
}
